<?php

class Seed extends Connect
{
     static $all = array();
     static $reload = false;
     
    
    static function getAll(){                       
        if(count(Seed::$all) > 0 && Seed::$reload != true) {
            return true;    
        }
                
        $sql = "SELECT  * FROM `".PREFIX_STATIC."seed` ";
        
        
        #return $sql;
        if(!$result = DBConnect::query($sql, 'seed')){
            return mysql_error();
        }
        Habars::getHabars();
        #$i = 0;
        $_arr = array();
        while($arr = DBConnect::getOne($result, 'seed')){
            #unset($arr['id']);
            $_arr[$arr['parent']] = $arr;
            $_arr[$arr['parent']]['id'] = $arr['parent'];
            if(!empty($arr['image'])){
                $_arr[$arr['parent']]['image'] = ( !Pet::is_url($arr['image']) ? MEDIA_URL.$arr['image'] : $arr['image']  );    
            }else{
                $_arr[$arr['parent']]['image'] = ( !Pet::is_url(@Habars::$habars[$arr['parent']]['image']) ? MEDIA_URL.@Habars::$habars[$arr['parent']]['image'] : @Habars::$habars[$arr['parent']]['image']  );
            }
            $_arr[$arr['parent']]['habar'] = @Habars::$habars[$arr['parent']];
                         
        }        
        Seed::$all = $_arr; 
        return true;
                
    }
    
    static function getOne($parent){
        if(Seed::$all[$parent]) return Seed::$all[$parent]; 
        $sql = "SELECT  * FROM `".PREFIX_STATIC."seed`  
                WHERE `parent` = '".$parent."' LIMIT 1";
        if(!$result = DBConnect::query($sql, 'seed:'.$parent)){
            return mysql_error();
        }
        Habars::getHabars();
        #$i = 0;
        $_arr = array();
        $arr = DBConnect::getOne($result, 'seed:'.$parent);
        if(!isset($arr['id'])) return false;
        #unset($arr['id']);
        $_arr[$arr['parent']] = $arr;
        $_arr[$arr['parent']]['id'] = $arr['parent'];
        $_arr[$arr['parent']]['image'] = ( !Pet::is_url($arr['image']) ? MEDIA_URL.$arr['image'] : $arr['image']  );
        $_arr[$arr['parent']]['habar'] = @Habars::$habars[$arr['parent']];
                         
        return $_arr; 
    }
    
    /**
      * 
     */
     function save($data){
        if( !is_array($data) ) return false;
        
        $sql = "UPDATE `".PREFIX_STATIC."seed` SET  
                                                `buy1` = '".$data['buy1']."', 
                                                `buy2` = '".$data['buy2']."', 
                                                `sell1` = '".$data['sell1']."', 
                                                `sell2` = '".$data['sell2']."',
                                                `descript` = '".$data['descript']."',
                                                `image` = '".str_replace(MEDIA_URL, '', $data['image']) ."' 
                                                WHERE `parent` = '".$data['id']."' ";
        #_echo($sql);        
        if(!DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }
        return true;
     }
     
     /**
      * 
     */
     function add($data){
        if( !is_array($data) ) return false;
        $sql = " INSERT INTO `".PREFIX_STATIC."seed` (`parent`, `buy1`,
                                                         `buy2`, `sell1`, `sell2`, `descript`)
                                                         VALUES
                                                         ('".$data['parent']."',                                                            
                                                          '".@$data['buy1']."', 
                                                          '".@$data['buy2']."', 
                                                          '".@$data['sell1']."', 
                                                          '".@$data['sell2']."',
                                                          '".@$data['descript']."')";        
        #return $sql;
        if(!DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }
        return true;
     }
     
     function del($parent){
        $del = "DELETE FROM `".PREFIX_STATIC."seed` WHERE `parent` = '".$parent."' ";
        if(!DBConnect::query($del)){
            $this->_error(mysql_error());
            return false;
        }
        return true;
     }
}
?>